// 封装axios
import axios from "axios";

const service = axios.create({
    baseURL: 'http://localhost:8080',
    timeout: 60000
})

// 添加请求拦截器
service.interceptors.request.use(config=>{
    console.log("请求拦截器被执行了");
    console.log(config);
    // 在请求的头部 添加token
    let token = sessionStorage.getItem("token");
    // let token = '111'
    config.headers['token'] = token
    return config;
},error => {
    console.log(error);
    return Promise.reject();
})
// 添加响应拦截器
service.interceptors.response.use(response=>{
    console.log('响应拦截器被执行了');
    console.log(response);
    if(response.data.status == false){
        // 证明token无效
        window.location.href = '/login'
    }
    return response;
},error => {
    console.log(error);
    return Promise.reject();
})

export default service;